package com.f8q8.agent.runner;

import com.f8q8.agent.entity.TaskEntity;
import com.f8q8.agent.enums.TaskTypeEnum;
import com.f8q8.agent.service.TaskService;
import com.f8q8.agent.utils.AndroidUtils;
import com.f8q8.agent.utils.SpringContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 任务执行线程
 *
 * @author zxl78
 * @version 1.0
 * @since 2023/12/05 0005 17:28:07
 */
@Slf4j
public class TaskRunnerThread implements Runnable{

    @Override
    public void run() {
        while (true) {
            try {
                TaskService taskService = SpringContextUtils.getBean(TaskService.class);
                // 如果任务是开启状态，就每次从列表中获取第0个任务执行
                if (taskService.getTaskFlag()) {
                    List<TaskEntity> taskEntities = taskService.findTaskList();
                    if (!CollectionUtils.isEmpty(taskEntities)) {
                        TaskEntity taskEntity = taskEntities.get(0);
                        Map<String, String> allDevices = taskService.findDeviceMap();
                        // 执行任务
                        if (Objects.equals(TaskTypeEnum.INPUT.getCode(), taskEntity.getType())) {
                            // 点击
                            log.info("点赞");
                            allDevices.forEach((k, v) -> {
                                AndroidUtils.click(k, taskEntity.getX(), taskEntity.getY());
                            });
                        } else {
                            log.info("自动输入内容");
                            // 输入内容
                            allDevices.forEach((k, v) -> {
                                AndroidUtils.click(k, 180, 2263);
                                try {
                                    Thread.sleep(1200L);
                                } catch (InterruptedException e) {
                                    throw new RuntimeException(e);
                                }
                                // 切换输入法
                                AndroidUtils.changeIme(k);
                                // 输入文字
                                AndroidUtils.input(k, taskEntity.getContent());
                                try {
                                    Thread.sleep(300L);
                                } catch (InterruptedException e) {
                                    throw new RuntimeException(e);
                                }
                                // 点击发送
                                AndroidUtils.click(k, 954, 2042);
                                try {
                                    Thread.sleep(100L);
                                } catch (InterruptedException e) {
                                    throw new RuntimeException(e);
                                }
                            });
                        }
                        // 任务执行完成，删除该元素
                        taskEntities.remove(taskEntity);
                    }
                }
                Thread.sleep(100L);
            } catch (InterruptedException e) {
                throw new RuntimeException(e.getMessage());
            }
        }
    }
}
